package com.gaoyu.regexparser;


import com.gaoyu.regexparser.Expression.Asterisk;
import com.gaoyu.regexparser.Expression.Constant;
import com.gaoyu.regexparser.Expression.Expression;
import com.gaoyu.regexparser.Expression.SubExpression.Brace;
import com.gaoyu.regexparser.Expression.SubExpression.Bracket;
import com.gaoyu.regexparser.Expression.SubExpression.Parentheses;
import com.gaoyu.regexparser.Expression.Plus;
import com.gaoyu.regexparser.Expression.QuestionMark;
import com.gaoyu.regexparser.Expression.SubExpression.SubExpression;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gaoyu on 2017/5/13.
 */

public class Pattern {

    private static String mReg;

    private static ArrayList<Expression> mExpressionList = new ArrayList<>();

    private Pattern() {

    }

    public Matcher matcher(String str) {
        return new Matcher(this, str);
    }

    /**
     * @param subExpression 子表达式
     * @param regEx 正则表达式字符串
     * @return 返回编译成功字符个数
     * */
    public int compile(SubExpression subExpression, String regEx) {
        List<Expression> childExps = subExpression.getChildExps();
        int index = 0;
        for (; index < regEx.length(); index ++) {
            char c = regEx.charAt(index);
            if (c == ',' || c == '-') continue;
            switch (c) {
                case '*' :
                    childExps.add(new Asterisk());
                    break;
                case '+' :
                    childExps.add(new Plus());
                    break;
                case '?' :
                    childExps.add(new QuestionMark());
                    break;
                case '(' :
                    SubExpression parentheses = new Parentheses();
                    index += compile(parentheses, regEx.substring(index + 1, regEx.length()));
                    childExps.add(parentheses);
                    break;
                case ')' :
                    return index + 1;
                case '|' :
                    break;
                case '[' :
                    SubExpression bracket = new Bracket();
                    index += compile(bracket, regEx.substring(index + 1, regEx.length()));
                    childExps.add(bracket);
                    break;
                case ']' :
                    return index + 1;
                case '{' :
                    SubExpression brace = new Brace();
                    index += compile(brace, regEx.substring(index + 1, regEx.length()));
                    childExps.add(brace);
                    break;
                case '}' :
                    return index + 1;
                default:
                    childExps.add(new Constant(c));
                    break;
            }
        }
        return index + 1;
    }

    private static SubExpression subExpression = new SubExpression();

    public Expression getExpression() {
        return subExpression;
    }

    public static Pattern compile(String regEx) {
        Pattern pattern = new Pattern();
        pattern.compile(subExpression, regEx);
        return pattern;
    }

}
